package com.thinksns.sociax.t4.android.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.thinksns.sociax.t4.unit.UnitSociax;


/**
 * Created by zhiyicx on 2017/6/2.
 */

public class BufferSeekBar extends View implements View.OnTouchListener {

    public static final int MAX_RATE = 10000;

    private int bottomColor = Color.GRAY,    //最底部进度
            middleColor = Color.GRAY,       //缓冲
            topColor = Color.WHITE;          //播放进度 and 小圆点

    private Paint paint;
    private int bottomHeight;
    private int pointRadius;
    private int playRate, bufferRate;
    private int width;
    private int mMaxRate = MAX_RATE;

    private onSeekBarChangeistener listener;

    public BufferSeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        paint.setStyle(Paint.Style.FILL);

        bottomHeight = UnitSociax.dip2px(context, 6);
        pointRadius = UnitSociax.dip2px(context, 6);
        playRate = 0;
        bufferRate = 0;

        setOnTouchListener(this);
    }

    public void setMax(int maxRate) {
        this.mMaxRate = maxRate;
    }

    public int getMax(){
        return mMaxRate;
    }

    public void updateProgress(int playRate) {
        this.playRate = playRate;
        invalidate();
    }

    public void updateBuffer(int bufferRate) {
        this.bufferRate = bufferRate;
        invalidate();
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(pointRadius + getPaddingLeft(), getPaddingTop() + pointRadius);
        paint.setColor(bottomColor);
        width = getWidth() - 2 * pointRadius;
        canvas.drawRect(0, 0, width, bottomHeight, paint);
        paint.setColor(middleColor);
        canvas.drawRect(0, 0, width * bufferRate / mMaxRate, bottomHeight, paint);
        paint.setColor(topColor);
        canvas.drawRect(0, 0, width * playRate / mMaxRate, bottomHeight, paint);
        canvas.drawCircle(width * playRate / mMaxRate, bottomHeight / 2, pointRadius, paint);
    }

    public void setOnSeekBarChangeListener(onSeekBarChangeistener listener) {
        this.listener = listener;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                playRate = (int) event.getX() * mMaxRate / width;
                listener.onStartTrackingTouch(this);
                listener.onProgressChanged(this, playRate);
                break;
            case MotionEvent.ACTION_MOVE:
                playRate = (int) event.getX() * mMaxRate / width;
                listener.onProgressChanged(this, playRate);
                break;
            case MotionEvent.ACTION_UP:
                playRate = (int) event.getX() * mMaxRate / width;
                listener.onStopTrackingTouch(this);
                listener.onProgressChanged(this, playRate);
                break;
        }
        invalidate();
        return true;
    }

    public interface onSeekBarChangeistener {
        /**
         * Notification that the progress level has changed.
         */
        void onProgressChanged(BufferSeekBar seekBar, int progress);

        /**
         * Notification that the user has started a touch gesture. Clients may want to use this
         * to disable advancing the seekbar.
         */
        void onStartTrackingTouch(BufferSeekBar seekBar);

        /**
         * Notification that the user has finished a touch gesture. Clients may want to use this
         * to re-enable advancing the seekbar.
         */
        void onStopTrackingTouch(BufferSeekBar seekBar);
    }
}
